package com.cloud.business.service.sys.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.business.dao.sys.*;
import com.cloud.business.entity.sys.*;
import com.cloud.business.dao.sys.SysMapCoefficientMapper;
import com.cloud.business.service.sys.ISysMapCoefficientService;
import com.cloud.business.entity.sys.SysMapCoefficient;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.mapcoefficient.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import com.cloud.core.common.HttpContentResult;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 地图系数配置 业务实现层
 * Copyright: Copyright (C) 2020 DLANGEL, Inc. All rights reserved.
 * Company: 大连安琪科技有限公司
 *
 * @author dh
 * @since 2020-10-12 02:53:20
 */
@Service
public class SysMapCoefficientServiceImpl extends ServiceImpl
        <SysMapCoefficientMapper, SysMapCoefficient> implements ISysMapCoefficientService {

    @Resource
    private SysMapCoefficientMapper sysMapCoefficientMapper;

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param queryModel
     * @return
     * @throws Exception
     */
    @Override
    public HttpContentResult getListByPage(SysMapCoefficientQueryModel queryModel) throws Exception {

        if (queryModel.getPageNum() > 0) {
            long startNum = (queryModel.getPageNum() - 1) * queryModel.getRecordNum();
            queryModel.setStartNum(startNum);
        }

        List<SysMapCoefficientModel> list = sysMapCoefficientMapper.getListByPage(queryModel);
        Long count = sysMapCoefficientMapper.getListCount(queryModel);

        return HttpContentResult.successResult("获取成功", count, list);
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param post
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysMapCoefficientPost post, ResultMsg msg) throws Exception {
        Date now = new Date();
        SysMapCoefficient sysMapCoefficient = new SysMapCoefficient();
        BeanUtils.copyProperties(post, sysMapCoefficient);
        sysMapCoefficient.setCreateTime(now);
        sysMapCoefficient.setModificationTime(now);
        int count = sysMapCoefficientMapper.insert(sysMapCoefficient);
        msg.setMsg("创建成功");
        return sysMapCoefficient.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param post
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysMapCoefficientPost post, ResultMsg msg) throws Exception {

        SysMapCoefficient sysMapCoefficient = sysMapCoefficientMapper.selectById(post.getObjectId());
        BeanUtils.copyProperties(post, sysMapCoefficient);
        Date now = new Date();
        sysMapCoefficient.setModificationTime(now);
        int count = sysMapCoefficientMapper.updateById(sysMapCoefficient);
        msg.setMsg("更新成功");
        return sysMapCoefficient.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysMapCoefficient getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysMapCoefficient sysMapCoefficient = sysMapCoefficientMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return sysMapCoefficient;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysMapCoefficientMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

}